package com.zyk.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zyk.entity.Organization;
import com.zyk.service.IOrganizationService;
import com.zyk.vo.FinalResult;
import com.zyk.vo.OrganizationVo;
import com.zyk.vo.OrganizationVo2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 组织机构 前端控制器
 * </p>
 *
 * @author feifeimao
 * @since 2017-11-20
 */
@Controller
@RequestMapping("/organization")
public class OrganizationController {


    @Autowired
    private IOrganizationService organizationService;

    /**
     * 跳转到部门管理页面
     */
    @RequestMapping("manager")
    public String manager(){
        return "admin/organization/organization";
    }

    /**
     * 跳转到部门添加页面
     */
    @RequestMapping("addPage")
    public  String addPage(){
        return "admin/organization/organizationAdd";
    }

    /**
     * 跳转到部门编辑页面
     */
    @RequestMapping("editPage")
    public  String editPage(Organization organization, Model model){
        final Organization organization1 = organizationService.selectById(organization.getId());
        model.addAttribute("organization",organization1);
        return "admin/organization/organizationEdit";
    }

    /**
     * tree
     *部门树，添加部门时，选择的上级部门
     * @return
     */
    @RequestMapping("tree")
    @ResponseBody
    public List<OrganizationVo> tree(){
        return organizationService.findAll();
    }

    /**
     * treeGrid
     *部门列表
     * @return
     */
    @ResponseBody
    @RequestMapping("treeGrid")
     public List<OrganizationVo2> treeGrid(){
     return organizationService.treeGrid();
    }

    /**
     * 添加部门
     */
    @RequestMapping("add")
    @ResponseBody
    public FinalResult add(Organization organization){
        organization.setCreateTime(new Date());
        if(organizationService.insert(organization)){
            return new FinalResult("添加成功",true);
        }else {
            return new FinalResult("添加失败",false);
        }
    }

    /**
     * 删除部门
     * @param organization
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public FinalResult delete(Organization organization){
        if(organizationService.deleteById(organization.getId())){
            return new FinalResult("删除成功",true);
        }else {
            return new FinalResult("删除失败",false);
        }
    }


    /**
     * edit
     * 编辑部门
     *
     */
    @RequestMapping("edit")
    @ResponseBody
    public FinalResult edit(Organization organization){
        return organizationService.edit(organization);
    }
}
